﻿using System;
using System.Data;
using Dapper;
using System.Linq;
using System.Configuration;
using System.Data.SqlClient;
using AuctionServer.Domain;
using System.Collections.Generic;

namespace AuctionServer
{
    internal sealed class BidService
    {
        private readonly string connectionstring;
        public static readonly BidService Instance = new BidService();
        private BidService()
        {
            connectionstring = ConfigurationManager.ConnectionStrings["SilenthinkAuction"].ConnectionString;
        }
        public UserInfo GetUserInfo(long id)
        {
            var a = new List<UserInfo>() { new UserInfo { Id = 1, Name = "test", NickName = "test", AuctionMoney = 500 }, new UserInfo { Id = 8, Name = "test2", NickName = "test2", AuctionMoney = 500 } };
            return a.FirstOrDefault(o => o.Id == id);
            //var builder = new SqlBuilder()
            //   .Field("*")
            //   .Table("")
            //   .Where("[Id]=@Id", new { id });
            //var template = builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopOne);
            //using (var connection = new SqlConnection(connectionstring))
            //{
            //    return connection.Query<UserInfo>(template).FirstOrDefault();
            //}
        }
        public IList<UserInfo> GetUserInfo()
        {
            var builder = new SqlBuilder()
                .Field("*")
                .Table("");
            //.Where("[Id]=@Id", new { id });
            var template = builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopOne);
            using (var connection = new SqlConnection(connectionstring).OpenConnection())
            {
                return connection.Query<UserInfo>(template).ToList();
            }
        }

        public AuctionInfo GetAuctionInfo(long id)
        {
            var builder = new SqlBuilder()
                .Field("*")
                .Table("[Auction]")
                .Where("[Id]=@Id", new { id });
            var template = builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopOne);
            using (var connection = new SqlConnection(connectionstring).OpenConnection())
            {
                return connection.Query<AuctionInfo>(template).FirstOrDefault();
            }
        }

        public IList<AuctionInfo> GetAuctionInfo()
        {
            var builder = new SqlBuilder()
                .Field("*")
                .Table("[View.AuctionInfo]");
            var template = builder.AddTemplate(BasicSingleTableSqlTemplate.Select);
            using (var connection = new SqlConnection(connectionstring).OpenConnection())
            {
                return connection.Query<AuctionInfo>(template).ToList();
            }
        }

        internal  void Bid(Bidding bidding, DateTime expectedTime)
        {
            try
            {
                var param = new DynamicParameters(bidding);
                param.AddDynamicParams(new{expectedTime});
                using (var connection = new SqlConnection(connectionstring).OpenConnection())
                {
                    connection.Execute("[Proc.Bid]", param, commandType: CommandType.StoredProcedure);
                }
            }
            catch (Exception e)
            {
                NLog.LogManager.GetCurrentClassLogger().Info(e.Message);
            }
        }
    }
}
